<template>
	<view class="cover bcf" style="overflow-y: auto;z-index: 9999999;height: 100vh;background-color: #EEE;" v-show="showStatus">
		<scroll ref="mescrollRef" @init="mescrollInit" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
			<cu-custom bgColor="bg-gradual-blue">
				<view slot="content">岗位</view>
			</cu-custom>
			<view class="bcf pos-r mart3 pad2 round2 shadow flrc" @tap.stop="selectIt(item.id,item.name)" v-for="item,index in data">
				<view class="padr3 fs16" v-show="multi">
					<text class="fa fa-circle-thin c12" v-if="!selectedIndex[index]"></text>
					<text class="fa fa-check-circle c01" v-if="selectedIndex[index]"></text>
				</view>
				<!-- <view class="pos-r flex1 bc13 pad1" @tap.stop="selectIt(item.id)"> -->
				<view class="pos-r flex1 pad1">
					<view>{{item.name}}</view>
					<ripple></ripple>
				</view>
			</view>
			<loading :loading="myIsLoading"></loading>
		</scroll>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import List from '../../assets/js/list.js'
	export default {
		mixins: [MescrollMixin],
		components: {},
		props: {
			scrollStatus: '',
			value: '',
			multi: {
				type:Boolean,
				default:false
			},
			show: false,
			all: {
				type:Boolean,
				default:true
			},
		},
		data() {
			let list = new List()
			return {
				list,
				where: {
					eq: {
						status: '',
						category: '',
						groups: '',
					}
				},
				search: "",
				data: [],
				checkVal: [],
				selected: [],
				selectedIndex: [],
				showStatus: false
			}
		},
		methods: {
			reload() {
				this.showLoading()
				this.list.setData({
					_order: {
						sort: "ASC"
					},
					_where: this.where,
					_search: this.search,
					_g:["job"],
					all:this.all
				}).reload()
			},
			doSearch() {
				this.showLoading()
				this.list.setData({
					_order: {
						id: "DESC"
					},
					_where: this.where,
					_search: this.search,
					_g:["job"],
					all:this.all
				}).reload()
			},
			selectIt(id,label) {
				// this.show = false
				if(!this.multi){
					this.showStatus = false
					this.$emit('input', '/api_/jobs/' + id)
					this.$emit('onSelect', '/api_/jobs/' + id,label)
					return false
				}
				this.checkBoxChange(id)
				
				
			},
			checkBoxChange(id) {
				if(this.$com.isInArray(id,this.selected)){
					this.selected = this.$com.arrayDelValue(this.selected,id)
					//todo 重构selected数据
				}else{
					this.selected.push(id)
				}
				this.selectedIndex = []
				this.data.map((data, index) => {
					this.selectedIndex.push(false)
					this.selected.map((d) => {
						if (data.id + '' == d) {
							this.$set(this.selectedIndex, index, true)
						}
					})
				})
			},
			selectAll() {
				let got = []
				for (let i in this.selectedIndex) {
					if(this.selectedIndex[i]){
						got.push('/api_/jobs/' + this.data[i].id)
					}
				}
				if(got.length == 0) {
					this.showStatus = false
					return
				}
				if(this.multi){
					this.$emit('input', got)
					this.$emit('onSelect', got)
				}else{
					this.$emit('input', got[0])
					this.$emit('onSelect', got[0])
				}
				this.showStatus = false
			},
			checkAll(){
				let count = 0
				for(let i in this.selectedIndex){
					if(this.selectedIndex[i]){
						count ++
					}
				}
				let bool = !(count === this.selectedIndex.length && count > 0)
				this.selectedIndex=[]
				this.selected=[]
				this.data.map((data, index) => {
					this.selectedIndex.push(bool)
					bool && this.selected.push(data.id)
				})
			}
		},
		mounted() {
			this.list.initMescroll(this).setUrl('/api/jobs').setData({
				_order: {
					sort: "ASC"
				},
				_where: this.where,
				_search: this.search,
				_g:["job"],
				all:this.all
			}).setCbFunc(res => {
				this.data = res
				this.clearLoading()
			}).reload()

		},
		onLoad() {

		},
		watch: {
			show(val, old) {
				this.selected = []
				this.selectedIndex = []
				this.reload()
				this.showStatus = this.$com.isbool(val)
			}
		}
	}
</script>

<style>

</style>
